﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.CustomerView.Common;
using LinkCard.Dao;
using LinkCard.Model;

namespace LinkCard.CustomerView
{
    public partial class Partners : System.Web.UI.Page
    {
        #region Variable declaration        

        private int PageSize = 100;
        private int CurrentPage = 1;
        private int TotalPageCount = 1;
        string keyword = "";
        #endregion

        public AppEnv _Instance = AppEnv.Instance();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            var tag = Page.RouteData.Values["keyword"];
            if (Request.Params["page"] != null)
                CurrentPage = Convert.ToInt32(Request.Params["page"]);

            if (tag != null)
            {
                keyword = tag.ToString();

                int curpage = 0;
                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }
                var s = new SupplierDao().GetSupplierByTag(keyword, curpage, PageSize,out TotalPageCount);
                rptSupplierCategory.DataSource = s;
                rptSupplierCategory.DataBind();
                //ltrPaging.Text = CustomerView.Common.Pager.Create(CurrentPage, TotalPageCount);
            }
            else
            {
                //LoadSuppliers();
                LoadSupplierCate();
            }
            Page.Title = "Đối tác - LinkCard";
            Page.MetaDescription = "Đối tác - LinkCard";
        }

        private void LoadSupplierCate()
        {
            var lst = new List<SupplierCategory>();

            var db = new TopLinkEntities();
            // Count pages
            //int suppliesCount = db.Supplier.Count();

            //int skip = currentPage*pageSize;
            //int skip = (currentPage - 1) * pageSize;
            lst = db.SupplierCategory.OrderBy(s => s.SupplierCateOrder).ToList();

            rptSupplierCategory.DataSource = lst;
            rptSupplierCategory.DataBind();
            
        }

        private List<SupplierObject> LoadSuppliers(string ParentID)
        {
            try
            {
                Dao.SupplierDao _suppliesDao = new Dao.SupplierDao();
                Dao.DealDao _dealDao = new Dao.DealDao();
                int supplierCateID = Convert.ToInt16(ParentID);
                List<LinkCard.Model.Supplier> _listSupplies = _suppliesDao.GetListPaged(CurrentPage, PageSize, ref TotalPageCount, supplierCateID);
                
                List<SupplierObject> lst = new List<SupplierObject>();
                foreach (var _supplier in _listSupplies)
                {
                    SupplierObject sTemp = new SupplierObject();

                    sTemp.ID = _supplier.SupplierID;
                    sTemp.Logo = HttpUtility.HtmlDecode(_supplier.Logo);
                    sTemp.Name = HttpUtility.HtmlDecode(_supplier.Name);
                    sTemp.LogoHome = HttpUtility.HtmlDecode(_supplier.LogoHome);
                    sTemp.Banner_Uudaidacbiet = HttpUtility.HtmlDecode(_supplier.Banner_Uudai);

                    List<Model.Deal> lastestDeal = new List<Model.Deal>();
                    var listDeal = _dealDao.GetAllBySupplier(_supplier.SupplierID);
                    if (listDeal != null && listDeal.Count > 0)
                        lastestDeal = _dealDao.GetLatestDealBySupplier(_supplier.SupplierID,1);

                    #region Popup Lastest Deal

                    if (lastestDeal.Count>0)
                    {
                        sTemp.LastDeal_Description = HttpUtility.HtmlDecode(lastestDeal[0].Description);
                        sTemp.LastDeal_Detail = HttpUtility.HtmlDecode(lastestDeal[0].Detail);
                        sTemp.LastDeal_Name = HttpUtility.HtmlDecode(lastestDeal[0].Name);
                        sTemp.LastDeal_ID = lastestDeal[0].DealID;

                        var album = Utils.Utilities.GetProductImage(lastestDeal[0].Image);

                        if (album != null && album.Count > 0)
                        {
                            if (album.Count>=1)
                                sTemp.LastDeal_Img1 = HttpUtility.HtmlDecode(album[0]);
                            if (album.Count>=2)
                                sTemp.LastDeal_Img2 = HttpUtility.HtmlDecode(album[1]);
                            if (album.Count>=3)
                                sTemp.LastDeal_Img3 = HttpUtility.HtmlDecode(album[2]);
                        }
                    }

                    #endregion

                    lst.Add(sTemp);
                }

                //rptSupplier.DataSource = lst;
                //rptSupplier.DataBind();
                // Show PagerControl
                ////ltrPaging.Text = CustomerView.Common.Pager.Create(CurrentPage, TotalPageCount);

                return lst;
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        public string GetSubString(object str, int length)
        {
            if (str==null)
                return "";

            else if (str.ToString().Length <= length)
                return str.ToString();
            else
                return (str.ToString().Substring(0, length) + "...");

        }

        public class SupplierObject
        {
            public long ID { get; set; }
            public string Logo { get; set; }
            public string LogoHome { get; set; }
            public string Banner_Uudaidacbiet { get; set; }
            public string Name { get; set; }

            public string LastDeal_Name { get; set; }
            public long LastDeal_ID { get; set; }
            public string LastDeal_Description { get; set; }
            public string LastDeal_Detail { get; set; }
            public string LastDeal_Img1 { get; set; }
            public string LastDeal_Img2 { get; set; }
            public string LastDeal_Img3 { get; set; }

            public SupplierObject()
            {
                ID = 0;
                Logo = "";
                Name = "";
                LogoHome = "";

                LastDeal_Name = "";
                LastDeal_ID = 0;
                LastDeal_Description = "";
                LastDeal_Detail = "";
                LastDeal_Img1 = "";
                LastDeal_Img2 = "";
                LastDeal_Img3 = "";
            }
        }

        protected void rptSupplierCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemIndex < 0) return;
            //DataRowView row = (DataRowView)e.Item.DataItem;
            
            var ParentID = DataBinder.Eval(e.Item.DataItem, "SupplierCateID").ToString();
            //var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            //var ParentID = row["SupplierCateID"].ToString();

            Repeater rp2 = (Repeater)e.Item.FindControl("rptSupplier");
            rp2.DataSource = LoadSuppliers(ParentID);
            rp2.DataBind();
            
        }
    }
}
